package net.java.nioserver.sample;

import net.java.nioserver.operations.OpAccept;
import net.java.nioserver.utils.ChannelUtil;
import net.jcip.annotations.ThreadSafe;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author Leonid Shlyapnikov
 */
@ThreadSafe
public class ConnectedOpAccept implements OpAccept {

    public static final String DEFAULT_RESPONSE = "Connected-->\n";

    private final String response;

    public ConnectedOpAccept() {
        this(DEFAULT_RESPONSE);
    }

    public ConnectedOpAccept(String resonse) {
        this.response = resonse;
    }

    public boolean handle(SocketChannel channel) throws IOException {
        ByteBuffer buf = ByteBuffer.wrap(response.getBytes());
        ChannelUtil.writeToChannel(buf, channel);

        return true;
    }

    public String toString() {
        return getClass().getSimpleName();
    }
}
